package cn.itsource.base.controller;

import cn.itsource.util.AjaxResult;
import cn.itsource.util.FastDfsUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Controller
@RequestMapping("/fastDfs")
public class FastDfsController {
    @RequestMapping("/upload")
    @ResponseBody
    public AjaxResult upload(MultipartFile file){
        //1.获取文件上传的原始名字
        String originalFilename = file.getOriginalFilename();
        //2.把后缀名截取出来
        String extName = originalFilename.substring(originalFilename.indexOf(".") + 1);
        //参数1：文件的数据 byte[]
        //参数2：文件的后缀名
        try {
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return  new AjaxResult().setSuccess(true).setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
}
